<?php /** It should be name func.php ? */

/**
 * 取用户自定义配置信息
 * 尝试去应用源码的conf目录,取用户自定义配置信息
 *
 * @param $value 临时改变缓存系统中的值(不能保存)
 */
function app_cfg($file, $key=null, $value=null)
{
	static $_cfg	=	array();

	if(!isset($_cfg[$file]))
	{
		try {
			$_cfg[$file]	=	import(Core::$APP_PATH.Core::$convent['conf_path'].
										'/'.$file.'.php');
		}
		catch(Exception $e)
		{
			$_cfg[$file]	=	null;
		}
	}
	
	if(null===$key)
	{
		return $_cfg[$file];
	}
	elseif($value!==null)
	{
		$_cfg[$file][$key]	=	$value;
	}
	else
	{
		return !empty($_cfg[$file][$key]) ? $_cfg[$file][$key] : null;
	}
}

function fetch_get($key, $type='string', $default=null) {
	return fetch_array($_GET, $key, $type, $default);
}
function fetch_post($key, $type='string', $default=null) {
	return fetch_array($_POST, $key, $type, $default);
}
function fetch_request($key, $type='string', $default=null) {
	return fetch_array($_REQUEST, $key, $type, $default);
}
function fetch_server($key, $type='string', $default=null) {
	return fetch_array($_SERVER, $key, $type, $default);
}
function fetch_a_int($key, $array=null){
	return fetch_array($array===null?$_REQUEST:$array, $key, 'int');
}
function fetch_a_string($key, $array=null){
	return fetch_array($array===null?$_REQUEST:$array, $key, 'string');
}
function fetch_a_char($key, $array=null){
	return fetch_array($array===null?$_REQUEST:$array, $key, 'char');
}
function fetch_a_float($key, $array=null){
	return fetch_array($array===null?$_REQUEST:$array, $key, 'float');
}
function fetch_a_array($key, $array=null){
	return fetch_array($array===null?$_REQUEST:$array, $key, 'array');
}
function fetch_array($array, $key, $type, $default=null) {
	switch ($type) {
	case 'string':
		return empty($array[$key]) ? ($default?$default:'') : trim(urldecode($array[$key]));
		break;
	case 'int':
		return empty($array[$key]) ? ($default?$default:0) : intval($array[$key]);
		break;
	case 'float':
		return empty($array[$key]) ? ($default?$default:0.0) : floatval($array[$key]);
		break;
	case 'array':
		return empty($array[$key]) ? ($default?$default:array()) : $array[$key];
		break;
	case 'char':
		return empty($array[$key]) ? ($default?$default:'') : $array[$key];
		break;
	default:
		return null;
	}
}

/**
 * URL生成函数，支持参数为数组或字符串。
 * url(array('a'=>'book-reader', 'id'=>12, 'type'=>334))
 * url('book-reader/id/12/type/334')
 *
 * @param $url string or array
 */
function url($url)
{
	if(is_array($url))
	{
		$t	=	array();
		if(!empty($url['a'])){
			$t[]	=	$url['a'];
			unset($url['a']);
		}
		else{
			$t[]	=	$_REQUEST['a'];
		}
		
		foreach($url as $k=>$v)
		{
			$t[]	=	$k.'/'.urlencode($v);
		}
		
		return WEB_ROOT. (Core::$convent['rewrite']?'/':'/?q=') .join('/', $t);
	}
	else
	{
		return WEB_ROOT. (Core::$convent['rewrite']?'/':'/?q=') .$url;
	}
}

/**
 * 删除指定目录及其下所有文件
 */
function dropdir($dir, $level=0)
{
	if(!is_dir($dir))
	{
		Log::error("Drop dir without regular: {$dir}, {$level}");
		return false;
	}
	if(!is_writeable($dir))
	{
		Log::error("Drop dir without writeable: {$dir}, {$level}");
		return false;
	}
	$dh	=	opendir($dir);
	if(!$dh){
		return false;
	}
	while($file=readdir($dh))
	{
		if($file=='.'||$file=='..'){
			continue;
		}
		if(!is_dir("{$dir}/{$file}"))
		{
			if(!@unlink("{$dir}/{$file}"))
			{
				Log::error("Drop dir without writeable: {$dir}, {$level}");
				return false;
			}
		}
		else
		{
			dropdir("{$dir}/{$file}", $level+1);
		}
	}
	closedir($dh);
	rmdir($dir);
	
	return true;
}

/**
 * Request by Ajax? Support JQuery.
 */
function is_ajax()
{
	return isset($_SERVER['HTTP_X_REQUESTED_WITH'])
		&& (0===strcmp('XMLHttpRequest', $_SERVER['HTTP_X_REQUESTED_WITH']))
		|| !empty($_REQUEST['ajax']);
}

function is_post(){
	return 0===strcmp($_SERVER['REQUEST_METHOD'], 'POST');
}

function build_request($extra=array()){
	return "/{$_REQUEST['a']}/". str_replace( array('=','&'), array('/','/'),
				http_build_query(array_merge(array_diff_key($_REQUEST, array('q'=>0,'a'=>0)), $extra)) );
}

function isprint(){
	return isset($_GET['viewtype']);
}


?>
